package com.ilop.sthome.ui.adapter.detail;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DefaultBean;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @Date on 2020-03-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class DefaultAdapter extends RecyclerView.Adapter<DefaultAdapter.ItemHolder> {

    private Context mContext;
    private List<DefaultBean> mList;
    private boolean isChange = false;

    public DefaultAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<DefaultBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setChange(boolean change) {
        isChange = change;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_default, viewGroup, false);
        return new ItemHolder(view);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DefaultBean bean = mList.get(i);
        if (isChange && "0C".equals(bean.getRecommendId())) {
            itemHolder.mMode.setVisibility(View.VISIBLE);
            itemHolder.mClick.setVisibility(View.GONE);
        }
        if (bean.getModify()){
            itemHolder.mClick.setVisibility(View.VISIBLE);
            AutomationBean mAutomation = AutomationDaoUtil.getInstance().findAutomationByRecommendId(bean);
            if (mAutomation == null){
                itemHolder.mClick.setImageResource(R.mipmap.check_unselected);
            }else {
                itemHolder.mClick.setImageResource(R.mipmap.check_selected);
            }
        }else {
            itemHolder.mClick.setVisibility(View.GONE);
        }
        itemHolder.mName.setText(ConfigureUtil.getCommonName(bean.getRecommendId()));
        itemHolder.mLogo.setImageResource(ConfigureUtil.getCommonImage(bean.getRecommendId()));
        itemHolder.mClick.setOnClickListener(view -> LiveDataBus.get().with(CommonId.COMMON_FUNCTION_CLICK).setValue(bean));
        itemHolder.mMode.setOnClickListener(view -> LiveDataBus.get().with(CommonId.DEFAULT_SCENE_CHANGE).setValue(bean));
        itemHolder.itemView.setOnClickListener(view -> {
            if (isChange && "0C".equals(bean.getRecommendId())) {
                LiveDataBus.get().with(CommonId.DEFAULT_SCENE_CHANGE).setValue(bean);
            }else {
                LiveDataBus.get().with(CommonId.DEFAULT_SCENE_CLICK).setValue(bean);
            }
        });
        itemHolder.itemView.setOnTouchListener((v, event) -> {
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    itemHolder.itemView.setScaleX(1.05f);
                    itemHolder.itemView.setScaleY(1.05f);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    itemHolder.itemView.setScaleX(1f);
                    itemHolder.itemView.setScaleY(1f);
                    break;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_default_logo)
        ImageView mLogo;
        @BindView(R.id.tv_default_name)
        TextView mName;
        @BindView(R.id.bt_default_change)
        Button mMode;
        @BindView(R.id.iv_default_click)
        ImageView mClick;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
